<script setup>
import api from '../api/staffapi.js'
import {ref, onMounted} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import lodash from 'lodash'

const isAdd=ref(false);
//定义绑定的数据
//表格中的数据
const tableData = ref([]);

const total=ref(0);
//定义对话框的显示状态
const editWinVisible = ref(false);
//查询数据
const searchObj = ref({
  current: 1,
  limit: 10,
  name: '',
  position: '',
  phone: ''
});

//要修改或者新增的数据
const obj = ref({});

const reseve=ref({});


const defaultImageUrl=ref('https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png');

const search = async () => {
  var result = await api.page(searchObj.value);
  tableData.value = result.data;
  console.log("表格数据：", tableData.value);
  total.value=result.count;
}
//页面加载后要挂载的数据
onMounted(async () => {
  await search();
});
//定义方法

const handleSizeChange=()=>{
  search();
}
const handleCurrentChange=()=>{
  search();
}

const handlerSearch = () => {
  searchObj.value.current = 1;
  search();
}

const resetSearch = () => {
  searchObj.value = {
    current: 1,
    limit: 10,
    name: '',
    position: '',
    phone: ''
  };
  search();
}
const handleUploadSuccess = (res) => {
  obj.value.image_url = res.data;
}

const save = async () => {
  try {
    if (isAdd.value) {
      // 新增操作
      await api.add(obj.value);
      ElMessage.success("新增成功");
    } else {
      // 编辑操作
      await api.edit(obj.value);
      ElMessage.success("修改成功");
    }
    await search(); // 刷新表格
    editWinVisible.value = false; // 关闭编辑对话框
  } catch (error) {
    console.error(isAdd.value ? "新增失败" : "编辑失败", error);
    ElMessage.error(isAdd.value ? "新增失败，请检查网络或数据格式" : "修改失败，请检查网络或数据格式");
  }
}

const del = (row) => {
  ElMessageBox.confirm(
      '确认删除该行数据吗?',
      '删除提示',
      {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'error',
      }
  )
      .then(async () => {
        await api.del(row.id);
        //重新加载数据
        //设置从第一页开始查询
        searchObj.value.current = 1;
        await search();
      })
      .catch(() => {
      })
}

const handlerAdd = async () => {
  obj.value = {name: '', position:'', phone: '', role_id: ''};
  isAdd.value=true;
  editWinVisible.value = true;
}

const edit = (row) => {
  obj.value = lodash.clone(row);
  isAdd.value = false;
  editWinVisible.value = true;
}

//图片加载失败处理
const handleImageError = (event) => {
  event.target.src = defaultImageUrl.value;
};
</script>

<template>
  <div class="cyber-container">
    <div class="cyber-card">
      <div class="card-header">
        <h2>员工管理</h2>
        <div class="header-actions">
          <el-button type="primary" @click="handlerAdd" class="cyber-button">
            <el-icon><Plus /></el-icon>
            新增员工
          </el-button>
        </div>
      </div>

      <div class="cyber-form-card">
        <el-form :model="searchObj" label-width="100px" class="search-form">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="姓名">
                <el-input v-model="searchObj.name" placeholder="请输入姓名" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="职位">
                <el-input v-model="searchObj.position" placeholder="请输入职位" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="电话">
                <el-input v-model="searchObj.phone" placeholder="请输入电话" clearable />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24" class="form-actions">
              <el-button type="primary" @click="search" class="cyber-button">
                <el-icon><Search /></el-icon>
                查询
              </el-button>
              <el-button @click="resetSearch" class="cyber-button">
                <el-icon><Refresh /></el-icon>
                重置
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="cyber-table-card">
        <el-table :data="tableData" style="width: 100%" border>
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="name" label="姓名" />
          <el-table-column prop="position" label="职位" />
          <el-table-column prop="phone" label="电话" />
          <el-table-column prop="role_id" label="角色ID" />
          <el-table-column label="照片" width="120">
            <template #default="scope">
              <el-avatar :src="scope.row.image_url" size="medium">
                <img src="https://bigbigyu.oss-cn-beijing.aliyuncs.com/b5a24a51-5db3-40ec-bffc-8999e9cfafc3.png" alt="默认头像" v-if="!scope.row.image_url" />
              </el-avatar>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template #default="scope">
              <el-button size="small" @click="edit(scope.row)" class="cyber-button">
                编辑
              </el-button>
              <el-button size="small" type="danger" @click="del(scope.row)" class="cyber-button danger">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination-container">
          <el-pagination
            v-model:current-page="searchObj.current"
            v-model:page-size="searchObj.limit"
            :page-sizes="[10, 20, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!-- 编辑/新增对话框 -->
    <el-dialog v-model="editWinVisible" :title="isAdd ? '新增员工' : '编辑员工'" width="600px" class="cyber-dialog">
      <div class="dialog-glow"></div>
      <el-form :model="obj" label-width="100px" class="edit-form">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="obj.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="obj.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="obj.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="角色ID" prop="role_id">
          <el-input v-model="obj.role_id" placeholder="请输入角色ID" />
        </el-form-item>
        <el-form-item label="头像" prop="image_url">
          <el-upload
              class="avatar-uploader"
              action="/api/upload/file"
              :show-file-list="false"
              :on-success="handleUploadSuccess"
          >
            <img :src="obj.image_url?obj.image_url:'https://xrysitu.oss-cn-beijing.aliyuncs.com/b5a24a51-5db3-40ec-bffc-8999e9cfafc3.png'"
                 style="width: 100px; height: 100px; border-radius: 50%;"
                 alt="用户头像"/>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="editWinVisible = false" class="cyber-button">取消</el-button>
          <el-button type="primary" @click="save" class="cyber-button primary">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.cyber-container {
  padding: 20px;
  min-height: calc(100vh - 40px);
  position: relative;
  overflow-y: auto;
}

.cyber-card {
  background: rgba(20, 20, 40, 0.7);
  border-radius: 12px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  border: 1px solid rgba(0, 243, 255, 0.2);
  backdrop-filter: blur(5px);
  position: relative;
  margin-bottom: 20px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid rgba(0, 243, 255, 0.2);
  background: rgba(15, 15, 30, 0.8);
  flex-shrink: 0;
}

.card-header h2 {
  color: #00f3ff;
  font-size: 22px;
  margin: 0;
  text-shadow: 0 0 10px rgba(0, 243, 255, 0.5);
}

.cyber-form-card {
  padding: 20px;
  background: rgba(25, 25, 45, 0.6);
  margin: 20px;
  border-radius: 8px;
  border: 1px solid rgba(0, 243, 255, 0.1);
  flex-shrink: 0;
}

.cyber-table-card {
  padding: 0 20px 20px;
  flex: 1;
  overflow-y: auto;
}

.search-form .el-form-item {
  margin-bottom: 15px;
}

.search-form .el-input__wrapper {
  background: rgba(10, 10, 25, 0.5);
  border: 1px solid rgba(0, 243, 255, 0.3);
  box-shadow: 0 0 5px rgba(0, 243, 255, 0.2);
}

.search-form .el-input__wrapper.is-focus {
  box-shadow: 0 0 10px rgba(0, 243, 255, 0.4);
}

.form-actions {
  display: flex;
  justify-content: flex-start;
  gap: 15px;
  margin-top: 10px;
}

.cyber-button {
  background: rgba(50, 50, 80, 0.7);
  border: 1px solid rgba(0, 243, 255, 0.5);
  color: #00f3ff;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.cyber-button:hover {
  background: rgba(0, 243, 255, 0.2);
  box-shadow: 0 0 15px rgba(0, 243, 255, 0.4);
  border-color: #00f3ff;
}

.cyber-button.primary {
  background: linear-gradient(90deg, #00f3ff, #00aaff);
  border: 1px solid #00f3ff;
  color: #000;
  font-weight: 600;
}

.cyber-button.primary:hover {
  background: linear-gradient(90deg, #00aaff, #00f3ff);
  box-shadow: 0 0 20px rgba(0, 243, 255, 0.6);
}

.cyber-button.danger {
  background: linear-gradient(90deg, #ff0055, #ff0000);
  border: 1px solid #ff0055;
  color: #fff;
}

.cyber-button.danger:hover {
  background: linear-gradient(90deg, #ff0000, #ff0055);
  box-shadow: 0 0 20px rgba(255, 0, 85, 0.6);
}

.cyber-table-card :deep(.el-table) {
  background: rgba(20, 20, 40, 0.7);
  border: 1px solid rgba(0, 243, 255, 0.2);
  color: #e0e0ff;
}

.cyber-table-card :deep(.el-table__header th) {
  background: rgba(15, 15, 30, 0.9);
  color: #00f3ff;
  font-weight: 600;
}

.cyber-table-card :deep(.el-table__row:hover) {
  background: rgba(0, 243, 255, 0.1);
}

.cyber-table-card :deep(.el-table__row) {
  background: rgba(25, 25, 45, 0.5);
}

.cyber-table-card :deep(.el-table__cell) {
  border-color: rgba(0, 243, 255, 0.1);
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
  flex-shrink: 0;
}

.cyber-dialog :deep(.el-dialog) {
  background: rgba(20, 20, 40, 0.95);
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid #00f3ff;
  box-shadow: 0 0 30px rgba(0, 243, 255, 0.3);
  backdrop-filter: blur(10px);
  position: relative;
}

.dialog-glow {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, transparent, #00f3ff, #ff00ff, transparent);
  z-index: 10;
}

.cyber-dialog :deep(.el-dialog__header) {
  background: rgba(10, 10, 25, 0.9);
  color: #ffffff;
  padding: 18px 20px;
  border-bottom: 1px solid rgba(0, 243, 255, 0.3);
  position: relative;
}

.cyber-dialog :deep(.el-dialog__title) {
  font-size: 18px;
  font-weight: 600;
  color: #ffffff;
  text-shadow: 0 0 5px rgba(0, 243, 255, 0.7);
}

.edit-form {
  padding: 20px;
}

.edit-form :deep(.el-form-item__label) {
  color: #00f3ff;
  text-shadow: 0 0 5px rgba(0, 243, 255, 0.5);
}

.edit-form :deep(.el-input__wrapper) {
  background: rgba(10, 10, 25, 0.5);
  border: 1px solid rgba(0, 243, 255, 0.3);
  box-shadow: 0 0 5px rgba(0, 243, 255, 0.2);
}

.edit-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 10px rgba(0, 243, 255, 0.4);
}

.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 10px;
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: rgba(25, 25, 45, 0.5);
  color: #00f3ff;
  font-size: 20px;
}
</style>
